package com.hj.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.hj.pojo.Teacher;
import com.hj.service.ITeacherService;
import com.hj.vo.PageResult;
import com.hj.vo.Result;

//@Controller
//@ResponseBody
@RestController
@RequestMapping(value = "/tea")
public class TeacherController {

    @Autowired
    ITeacherService teacherService;

    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") Integer page
    		, @RequestParam(defaultValue = "3") Integer size
    		, Teacher teacher) {
    	
    	PageInfo<Teacher> pageInfo = teacherService.list(page, size, teacher);
    	PageResult pr = new PageResult();
    	pr.setData(pageInfo.getList());
    	pr.setRow(pageInfo.getTotal());
    	
    	Result result = new Result();
        result.setCode(200);
        result.setMsg("ok");
        result.setData(pr);
    	
        return result;
    }

    @PostMapping("/add")
    public Result add(@RequestBody Teacher teacher) {
        teacherService.add(teacher);
        Result result = new Result();
        result.setCode(200);
        result.setMsg("ok");
        
        if (StringUtils.isEmpty(teacher.gettName())) {
        	result.setCode(202);
            result.setMsg("请输入姓名");
		}
        if (StringUtils.isEmpty(teacher.getPassword())) {
        	result.setCode(202);
        	result.setMsg("请输入密码");
        }
        
        return result;
    }
    
    @PutMapping("/update")
    public Result update(@RequestBody Teacher teacher) {
        teacherService.update(teacher);
        Result result = new Result();
        result.setCode(200);
        result.setMsg("ok");
        return result;
    }

    @DeleteMapping("/{sid}")
    public Result del(@PathVariable("sid") String id) {
        teacherService.delete(id);
        Result result = new Result();
        result.setCode(200);
        result.setMsg("ok");
        return result;
    }
    
    @GetMapping("/{sid}")
    public Result findOne(@PathVariable("sid")String id) throws Exception {
        Result result = new Result();
        result.setCode(200);
        result.setMsg("ok");
        
        if (id.equals("-1")) {
        	result.setCode(202);
            result.setMsg("非法参数");
            return result;
		}
        
        result.setData(teacherService.findOne(id));
        return result;
    }

}
